package com.mouse.hxsj.view;

import com.mouse.damo.micro.Com;
import com.mouse.damo.micro.Sleep;
import com.mouse.hxsj.constants.ComPool;
import com.mouse.hxsj.entity.common.Hwnd;
import com.mouse.hxsj.service.ActionService;
import com.mouse.hxsj.util.ViewGet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;

@Slf4j
@Component
public class EvocationFrm extends JInternalFrame {
    private JComboBox comboBox_tree1;
    private JComboBox comboBox_double1;
    private JComboBox comboBox_most1;
    private JComboBox<Hwnd> comboBox_hwnd1;
    private JComboBox comboBox_up1;
    private JButton button_1;

    private JComboBox comboBox_tree2;
    private JComboBox comboBox_double2;
    private JComboBox comboBox_most2;
    private JComboBox<Hwnd> comboBox_hwnd2;
    private JComboBox comboBox_up2;
    private JButton button_2;


    private JComboBox comboBox_tree3;
    private JComboBox comboBox_double3;
    private JComboBox comboBox_most3;
    private JComboBox<Hwnd> comboBox_hwnd3;
    private JComboBox comboBox_up3;
    private JButton button_3;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    EvocationFrm frame = new EvocationFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public EvocationFrm() {
        setClosable(true);
        setIconifiable(true);
        setTitle("招魂");
        setBounds(20, 20, 583, 397);

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "账号1", TitledBorder.LEADING, TitledBorder.TOP, null, null));

        JLabel label_2 = new JLabel("双倍");

        JLabel lblf = new JLabel("变树(F4)");

        comboBox_tree1 = new JComboBox();
        comboBox_tree1.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));

        comboBox_double1 = new JComboBox();
        comboBox_double1.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));

        JLabel lblf_1 = new JLabel("多倍15m(F5)");

        comboBox_most1 = new JComboBox();
        comboBox_most1.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));

        JLabel label_1 = new JLabel("句柄");

        comboBox_hwnd1 = new JComboBox();

        comboBox_up1 = new JComboBox();
        comboBox_up1.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));

        JLabel label_3 = new JLabel("65升级");

        button_1 = new JButton("开启");
        button_1.addActionListener(e -> run(comboBox_up1, comboBox_double1, comboBox_tree1, comboBox_most1, comboBox_hwnd1, button_1, 0));
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(comboBox_up1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                                .addGap(8)
                                                .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(comboBox_double1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(lblf, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                                                .addGap(4)
                                                .addComponent(comboBox_tree1, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addComponent(lblf_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(comboBox_most1, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(label_1, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(comboBox_hwnd1, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)
                                                .addGap(26)))
                                .addGap(9)
                                .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
                                .addGap(20))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(comboBox_double1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_2)
                                        .addComponent(lblf)
                                        .addComponent(comboBox_tree1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_3)
                                        .addComponent(comboBox_up1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button_1))
                                .addGap(18)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblf_1)
                                        .addComponent(comboBox_most1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_1)
                                        .addComponent(comboBox_hwnd1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(12, Short.MAX_VALUE))
        );
        panel.setLayout(gl_panel);

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new TitledBorder(null, "账号2", TitledBorder.LEADING, TitledBorder.TOP, null, null));

        JLabel label = new JLabel("句柄");

        comboBox_hwnd2 = new JComboBox();
        JLabel label_4 = new JLabel("双倍");

        comboBox_double2 = new JComboBox();
        comboBox_double2.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));
        JLabel label_5 = new JLabel("变树(F4)");

        comboBox_tree2 = new JComboBox();
        comboBox_tree2.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));
        JLabel label_6 = new JLabel("65升级");

        comboBox_up2 = new JComboBox();
        comboBox_up2.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));
        JLabel label_7 = new JLabel("多倍15m(F5)");

        comboBox_most2 = new JComboBox();
        comboBox_most2.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));

        button_2 = new JButton("开启");
        button_2.addActionListener(e -> run(comboBox_up2, comboBox_double2, comboBox_tree2, comboBox_most2, comboBox_hwnd2, button_2, 4));
        GroupLayout gl_panel_1 = new GroupLayout(panel_1);
        gl_panel_1.setHorizontalGroup(
                gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_1.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(label_7, GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(comboBox_most2, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(comboBox_hwnd2, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)
                                                .addGap(36))
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(comboBox_up2, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                                .addGap(8)
                                                .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(comboBox_double2, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                                                .addGap(4)
                                                .addComponent(comboBox_tree2, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(button_2, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
                                .addGap(24))
        );
        gl_panel_1.setVerticalGroup(
                gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_1.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(comboBox_double2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_4)
                                        .addComponent(label_5)
                                        .addComponent(comboBox_tree2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_6)
                                        .addComponent(comboBox_up2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button_2))
                                .addGap(18)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_7)
                                        .addComponent(comboBox_most2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label)
                                        .addComponent(comboBox_hwnd2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(12, Short.MAX_VALUE))
        );
        panel_1.setLayout(gl_panel_1);

        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new TitledBorder(null, "账号3", TitledBorder.LEADING, TitledBorder.TOP, null, null));

        JLabel label_8 = new JLabel("句柄");

        comboBox_hwnd3 = new JComboBox();
        JLabel label_9 = new JLabel("双倍");

        comboBox_double3 = new JComboBox();
        comboBox_double3.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));
        JLabel label_10 = new JLabel("变树(F4)");

        comboBox_tree3 = new JComboBox();
        comboBox_tree3.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));
        JLabel label_11 = new JLabel("65升级");

        comboBox_up3 = new JComboBox();
        comboBox_up3.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));
        JLabel label_12 = new JLabel("多倍15m(F5)");

        comboBox_most3 = new JComboBox();
        comboBox_most3.setModel(new DefaultComboBoxModel(new String[]{"是", "否"}));

        button_3 = new JButton("开启");
        button_3.addActionListener(e -> run(comboBox_up3, comboBox_double3, comboBox_tree3, comboBox_most3, comboBox_hwnd3, button_3, 8));
        GroupLayout gl_panel_2 = new GroupLayout(panel_2);
        gl_panel_2.setHorizontalGroup(
                gl_panel_2.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_2.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING)
                                        .addGroup(gl_panel_2.createSequentialGroup()
                                                .addComponent(label_11, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(comboBox_up3, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                                .addGap(8)
                                                .addComponent(label_9, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(comboBox_double3, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(label_10, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                                                .addGap(4)
                                                .addComponent(comboBox_tree3, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18))
                                        .addGroup(gl_panel_2.createSequentialGroup()
                                                .addComponent(label_12, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(comboBox_most3, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(label_8, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(comboBox_hwnd3, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)
                                                .addGap(26)))
                                .addGap(11)
                                .addComponent(button_3, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
                                .addGap(20))
        );
        gl_panel_2.setVerticalGroup(
                gl_panel_2.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_2.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(comboBox_double3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_9)
                                        .addComponent(label_10)
                                        .addComponent(comboBox_tree3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_11)
                                        .addComponent(comboBox_up3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button_3))
                                .addGap(18)
                                .addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_12)
                                        .addComponent(comboBox_most3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_8)
                                        .addComponent(comboBox_hwnd3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(12, Short.MAX_VALUE))
        );
        panel_2.setLayout(gl_panel_2);
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(panel_1, 0, 0, Short.MAX_VALUE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 534, Short.MAX_VALUE)))
                                .addGap(23))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(56, Short.MAX_VALUE))
        );
        getContentPane().setLayout(groupLayout);

    }
    private Thread doubleThread = null, shenMuThread = null, shengjiThread = null, mostThread = null;

    private void run(JComboBox comboBox_up, JComboBox comboBox_double, JComboBox comboBox_tree,
                     JComboBox comboBox_most, JComboBox comboBox_hwnd, JButton button, Integer houer) {
        String text = button.getText();
        if (text.equals("开启")) {
            button.setText("关闭");
            String upStr = (String) ViewGet.comboBox(comboBox_up, "");
            String doubleStr = (String) ViewGet.comboBox(comboBox_double, "");
            String treeStr = (String) ViewGet.comboBox(comboBox_tree, "");
            String mostStr = (String) ViewGet.comboBox(comboBox_most, "");
            String hwndStr = ViewGet.comboBox(comboBox_hwnd, "hwnd", String.class);
            Integer hwnd = Integer.valueOf(hwndStr);

            Com com = ComPool.getByHwmd(hwnd);
            if (com == null) return;
            log.info("开始招魂");
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.HOUR_OF_DAY, houer);
            if (doubleStr.equals("是")) {
                doubleThread = ActionService.double2(com);
                Sleep.sleep(200);
            }
            shengjiThread = ActionService.shengJiJob(com, upStr.equals("是"));
            if (mostStr.equals("是")) {
                mostThread = ActionService.most(com);
                Sleep.sleep(200);
            }
            if (treeStr.equals("是")) {
                shenMuThread = ActionService.shenMu(com, instance.getTime());
            }
        } else if (text.equals("关闭")) {
            button.setText("开启");
            if (doubleThread != null) doubleThread.stop();
            if (shenMuThread != null) shenMuThread.stop();
            if (shengjiThread != null) shengjiThread.stop();
            if (mostThread != null) mostThread.stop();
        }
    }

    public void initFrm() {
        java.util.List<Com> comList = ComPool.getComList();
        List<String> accountList = ComPool.getAccountList();
        comboBox_hwnd1.removeAllItems();
        comboBox_hwnd2.removeAllItems();
        comboBox_hwnd3.removeAllItems();
        for (int i = 0; i < comList.size(); i++) {
            Hwnd hwnd = new Hwnd();
            hwnd.setHwnd(comList.get(i).getHwnd());
            hwnd.setAccount(accountList.get(i));
            comboBox_hwnd1.addItem(hwnd);
            comboBox_hwnd2.addItem(hwnd);
            comboBox_hwnd3.addItem(hwnd);
        }
    }
}
